package com.aidong.web.controller.card;

/**
 * Created by john on 16-11-3.
 */
import com.aidong.cache.NameItem;
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.GiftCardDao;
import com.aidong.model.wills.GiftCard;
import com.aidong.web.controller.BaseController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import jxl.Sheet;
import jxl.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"giftCard"})
public class GiftCardController extends BaseController {
    @Resource
    GiftCardDao giftCardDao;

    public GiftCardController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("giftcard/card/list")).addObject("shopName", this.getTypeList("shopName")).addObject("cardType", this.getTypeList("giftCardType")).addObject("cardStatus", this.getTypeList("cardStatus"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "cardType",required = false) String cardType, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "noFrom",required = false) String noFrom, @RequestParam(value = "noTo",required = false) String noTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 50);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "shopCode", shopCode);
        cd.eq("cardType", cardType);
        cd.eq("status", status);
        cd.ge("cardCode", noFrom);
        cd.le("cardCode", noTo);
        this.giftCardDao.queryPageList(pg, cd, (new OrderSet()).asc("cardCode"));
        ModelAndView mv = new ModelAndView("giftcard/card/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        ModelAndView mv = new ModelAndView("giftcard/card/add");
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("cardType", this.getTypeList("giftCardType"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@RequestParam("codes") String codes) {
        ArrayList list = new ArrayList();
        ArrayList ccode = new ArrayList();
        String[] ss = codes.split("\\|");
        String[] e = ss;
        int len$ = ss.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            String ss1 = e[i$];
            String[] s1 = ss1.split(",");
            String shopCode = this.trim(s1[0]);
            String cardCode = this.trim(s1[1]);
            String cardType = this.trim(s1[2]);
            if(this.cache.getValue("shopName", shopCode) == null) {
                return this.getResult(false, "门店编号错误-" + shopCode);
            }

            if(cardCode.length() != 19) {
                return this.getResult(false, "卡号错误,不是19位-" + cardCode);
            }

            NameItem ctype = this.cache.getNameItem("giftCardType", cardType);
            if(ctype == null) {
                return this.getResult(false, "卡类型错误-" + cardType);
            }

            GiftCard card = new GiftCard();
            card.setShopCode(shopCode);
            card.setCardCode(cardCode);
            card.setCardType(ctype.getId());
            card.setCardAmount(Double.valueOf(Double.parseDouble(ctype.getProp())));
            list.add(card);
            ccode.add(cardCode);
            if(ccode.size() >= 100) {
                String c = this.giftCardDao.chkCardCodes(ccode);
                if(c != null) {
                    return this.getResult(false, "卡号已经存在-" + c);
                }

                ccode.clear();
            }
        }

        if(ccode.size() > 0) {
            String var17 = this.giftCardDao.chkCardCodes(ccode);
            if(var17 != null) {
                return this.getResult(false, "卡号已经存在-" + var17);
            }

            ccode.clear();
        }

        try {
            this.giftCardDao.addCardList(list, this.getLoginUserCode());
            this.addUserLog("储值卡配卡", "" + list.size() + "张卡");
        } catch (Exception var16) {
            this.log.error("add giftcard", var16);
            return this.getResult(false, "保存发生错误");
        }

        return this.getSuccess(String.valueOf(list.size()), "生成了" + list.size() + "张卡");
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("id") String ids) {
        String[] ss = ids.split(",");
        ArrayList list = new ArrayList();
        String[] e = ss;
        int len$ = ss.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            String c = e[i$];
            GiftCard card = this.giftCardDao.getByCardCode(c);
            if(card == null) {
                return this.getResult(false, "没有这个卡号:" + c);
            }

            if(card.getStatus().intValue() != 1) {
                return this.getResult(false, "已使用,不能删除:" + c);
            }

            if(card.getReportLoss().intValue() == 1) {
                return this.getResult(false, "已挂失,不能删除:" + c);
            }

            list.add(card.getCardCode());
        }

        if(list.size() == 0) {
            return this.getResult(false, "请选择卡号");
        } else {
            try {
                this.giftCardDao.delCardList(list);
                this.addUserLog("储值卡删除配卡", list.size() + "张卡");
            } catch (Exception var9) {
                this.log.error("del giftcard", var9);
                return this.getResult(false, "删除发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }

    @RequestMapping({"_upload"})
    @ResponseBody
    public String _upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        if(file.isEmpty()) {
            return this.getResult(false, "请上传文件");
        } else {
            String fileName = file.getOriginalFilename();
            String ext = this.getFileExt(fileName);
            if(!"xls".equals(ext) && !"xlsx".equals(ext)) {
                return this.getResult(false, "只能上传Excel文件(xls,xlsx)");
            } else {
                ArrayList list = new ArrayList();

                try {
                    Workbook sb = Workbook.getWorkbook(file.getInputStream());
                    Sheet i$ = sb.getSheet(0);
                    int n = i$.getRows();
                    List ls = this.cache.getItemList("giftCardType");
                    HashMap tmap = new HashMap();
                    Iterator shopCode = ls.iterator();

                    while(shopCode.hasNext()) {
                        NameItem cardCode = (NameItem)shopCode.next();
                        tmap.put(cardCode.getProp(), cardCode);
                    }

                    for(int card = 1; card < n; ++card) {
                        String var22 = this.trim(i$.getCell(0, card).getContents());
                        String var23 = this.trim(i$.getCell(1, card).getContents());
                        String mm = this.trim(i$.getCell(2, card).getContents());
                        if(this.cache.getValue("shopName", var22) == null) {
                            return this.getResult(false, "门店编号错误-" + var22);
                        }

                        if(var23.length() != 19) {
                            return this.getResult(false, "卡号错误,不是19位-" + var23);
                        }

                        NameItem cardType = (NameItem)tmap.get(mm);
                        if(cardType == null) {
                            return this.getResult(false, "卡金额错误-" + mm);
                        }

                        GiftCard card1 = new GiftCard();
                        card1.setShopCode(var22);
                        card1.setCardCode(var23);
                        card1.setCardType(cardType.getId());
                        list.add(card1);
                    }

                    sb.close();
                } catch (Exception var18) {
                    this.log.error("_uploadmb", var18);
                    return this.getResult(false, "文件格式错误");
                }

                if(list.size() == 0) {
                    return this.getResult(false, "没有会员信息");
                } else {
                    StringBuilder var19 = new StringBuilder("[");

                    GiftCard var21;
                    for(Iterator var20 = list.iterator(); var20.hasNext(); var19.append("{shopCode:\"").append(var21.getShopCode()).append("\"").append(",shopName:\"").append(this.nvl(this.cache.getValue("shopName", var21.getShopCode()))).append("\"").append(",cardCode:\"").append(this.nvl(var21.getCardCode())).append("\"").append(",cardType:\"").append(var21.getCardType()).append("\"").append(",typeName:\"").append(this.nvl(this.cache.getValue("giftCardType", var21.getCardType()))).append("\"").append(",cardInfo:\"").append(var21.getShopCode() + "," + var21.getCardCode() + "," + var21.getCardType()).append("\"").append("}")) {
                        var21 = (GiftCard)var20.next();
                        if(var19.length() > 2) {
                            var19.append(",");
                        }
                    }

                    var19.append("]");
                    return this.getSuccess(var19.toString(), "");
                }
            }
        }
    }

    private String getFileExt(String fileName) {
        if(fileName != null) {
            int pos = fileName.lastIndexOf(".");
            if(pos > 0) {
                return fileName.substring(pos + 1).toLowerCase();
            }
        }

        return null;
    }
}
